import facade
import requests
from xjlibrary.our_file_dir import BaseDir
from xjlibrary.tools.BaseUrl import BaseUrl

curPath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curPath, -2)
sPath = BaseDir.get_new_path(TopPath, "download", "sublibbook", "download", "books")
BaseDir.create_dir(sPath)

class DownBooks(object):

    def __init__(self):
        self.logger = facade.get_streamlogger()
        self.Baseurl = "http://202.115.72.61:9099/"
        self.mysqlutils = facade.MysqlUtiles(BaseDir.get_new_path(curPath, "db.ini"), "db", self.logger)
        self.proxy = {
            "http": "192.168.30.176:8131",
            "https": "192.168.30.176:8131",
        }
        self.sn = requests.Session()
        self.headers = {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
            "Cache-Control": "no-cache",
            "Host": "202.115.72.61:9099",
            "Referer": "http://202.115.72.61:9099/",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"
        }

    def select(self):
        sql = "select url from books where stat=0"
        rows = self.mysqlutils.SelectFromDB(sql)
        for row in rows:
            url = row[0]
            bookID = BaseUrl.urlQuery2Dict(url)["bookID"]
            pagespath = BaseDir.get_new_path(sPath, bookID + ".html")
            if BaseDir.is_file_exists(pagespath):
                sql = "update books set stat=1 where url = '{}'".format(url)
                self.mysqlutils.ExeSqlToDB(sql)
                self.logger.info("文件存在："+pagespath)
                continue
            BoolResult, errString, r = facade.BaseRequest(url,
                                                          sn=self.sn,
                                                          mark=bookID,
                                                          proxies=self.proxy,
                                                          headers=self.headers,
                                                          timeout=(30, 60))
            if BoolResult:
                BaseDir.single_write_file(r.text, pagespath)
                sql = "update books set stat=1 where url = '{}'".format(url)
                self.mysqlutils.ExeSqlToDB(sql)
            else:
                sql = "update books set failcount=failcount+1 where url = '{}'".format(url)
                self.mysqlutils.ExeSqlToDB(sql)


if __name__ == "__main__":
    down = DownBooks()
    down.select()